<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['sheBeiMingCheng', validatorRules.sheBeiMingCheng]"
                placeholder="请输入设备名称"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="设备类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['sheBeiLeiBie', validatorRules.sheBeiLeiBie]"
                :trigger-change="true"
                dictCode="gkak_sblb"
                placeholder="请选择设备类别"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="设备序列号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['sheBeiXuLieHao', validatorRules.sheBeiXuLieHao]"
                placeholder="请输入设备序列号"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="设备参数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['sheBeiCanShu', validatorRules.sheBeiCanShu]"
                placeholder="请输入设备参数"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="设备属性" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['sheBeiShuXing', validatorRules.sheBeiShuXing]"
                placeholder="请输入设备属性"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="发放状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['faFangZhuangTai', validatorRules.faFangZhuangTai]"
                :trigger-change="true"
                dictCode="gkak_ffzt"
                placeholder="请选择发放状态"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="归档人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['guiDangRen', validatorRules.guiDangRen]"
                placeholder="请输入归档人"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="归档时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择归档时间"
                v-decorator="['guiDangShiJian', validatorRules.guiDangShiJian]"
                :trigger-change="true"
                style="width: 100%"
                :disabled="disableSubmit"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="厂家信息" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['changJiaXinXi', validatorRules.changJiaXinXi]"
                placeholder="请输入厂家信息"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="机构类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['jiGouType', validatorRules.jiGouType]"
                :trigger-change="true"
                dictCode="gkak_sbjglx"
                placeholder="请选择机构类型"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="所属机构" :disabled="disableSubmit" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <j-select-d
          epart v-decorator="['jiGouId', validatorRules.jiGouId]" :trigger-change="true" /> -->
              <j-select-jigou
                :disabled="disableSubmit"
                :msg="jiGouIds"
                v-decorator="['jiGouId', validatorRules.jiGouId]"
                :trigger-change="true"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="居民ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-inFo
                :disabled="disableSubmit"
                :msg="dataId"
                v-decorator="['jianKangXinXiId', validatorRules.jianKangXinXiId]"
                :trigger-change="true"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="备注信息" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['beiZhuXinXi', validatorRules.beiZhuXinXi]"
                placeholder="请输入备注信息"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
// import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
import JSelectJigou from '@/components/jeecgbiz/JSelectJigou'
import JSelectInFo from '@/components/jeecgbiz/JSelectInFo'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
// import JSelectProduct from '@/components/jeecgbiz/JSelectProduct'

export default {
  name: 'GkakSbSheBeiXinXiModal',
  components: {
    JDate,
    JSelectJigou,
    JSelectInFo,
    JDictSelectTag
  },
  data() {
    return {
      disableSubmit: false,
      dataId: {},
      jiGouIds: {},
      // jiGou:"",
      form: this.$form.createForm(this),
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        sheBeiMingCheng: { rules: [{ required: true, message: '请输入设备名称!' }] },
        sheBeiLeiBie: { rules: [{ required: true, message: '请输入设备类别!' }] },
        sheBeiXuLieHao: { rules: [{ required: true, message: '请输入设备序列号!' }] },
        sheBeiCanShu: { rules: [] },
        sheBeiShuXing: { rules: [] },
        faFangZhuangTai: { rules: [{ required: true, message: '请输入发放状态!' }] },
        guiDangRen: { rules: [] },
        guiDangShiJian: { rules: [] },
        changJiaXinXi: { rules: [] },
        beiZhuXinXi: { rules: [] },
        jiGouType: { rules: [] },
        jiGouId: { rules: [] },
        jianKangXinXiId: {
          rules: []
        }
      },
      url: {
        add: '/gkakSbSheBeiXinXi/add',
        edit: '/gkakSbSheBeiXinXi/edit'
      }
    }
  },
  created() {},
  methods: {
    inFoId(record) {
      this.$nextTick(() => {
        this.dataId = {
          id: record.jianKangXinXiId,
          name: record.xingMing
        }
      })
    },
    jiGouId(record) {
      this.$nextTick(() => {
        this.jiGouIds = {
          id: record.xingMing,
          name: record.jiGouMingCheng
        }
      })
    },
    add() {
      this.edit({
        
      })
    },
    edit(record) {
     
      if (this.$route.query.jianKangXinXiId) {
        if (localStorage.getItem('jiangkangInfo')) {
          let row = JSON.parse(localStorage.getItem('jiangkangInfo'))
          console.log(row)
         this.dataId = {
          id: row.id,
          name:row.xingMing
        }
        }
      } else {
        this.inFoId(record)
        this.jiGouId(record)
      }
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'sheBeiMingCheng',
            'sheBeiLeiBie',
            'sheBeiXuLieHao',
            'sheBeiCanShu',
            'sheBeiShuXing',
            'faFangZhuangTai',
            'guiDangRen',
            'guiDangShiJian',
            'changJiaXinXi',
            'beiZhuXinXi',
            'jiGouType',
            'jiGouId',
            'jianKangXinXiId'
          )
        )
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },

    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        debugger
        if (!err) {
          let val = this.$route.query.jianKangXinXiId ? this.$route.query.jianKangXinXiId : values.jianKangXinXiId
          if (val) {
            values.jianKangXinXiId = val
          }

          that.confirmLoading = true
          let httpurl = ''
          let method = ''

          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'sheBeiMingCheng',
          'sheBeiLeiBie',
          'sheBeiXuLieHao',
          'sheBeiCanShu',
          'sheBeiShuXing',
          'faFangZhuangTai',
          'guiDangRen',
          'guiDangShiJian',
          'changJiaXinXi',
          'beiZhuXinXi',
          'jiGouType',
          'jiGouId',
          'jianKangXinXiId'
        )
      )
    }
  }
}
</script>
